Ext.onReady(function(){
    var editUrl='../hf/gridBase.do?clazz=RechoningManage&action=ajaxEdit';
    var deleteUrl='../hf/gridBase.do?clazz=RechoningManage&action=ajaxDelete';
    var listUrl='../hf/gridBase.do?action=ajaxList&clazz=RechoningManage';
    var formSaveUrl='../hf/gridBase.do?clazz=RechoningManage&action=ajaxSave';
    var freezeUrl='../hf/gridBase.do?clazz=RechoningManage&action=ajaxFreeze';
    var baseUrl='../hf/accountManager.do';
    
    var accountUrl='../hf/gridBase.do?clazz=RechoningManage&action=ajaxList';
    var setDefaultAccountUrl='../hf/gridBase.do?clazz=RechoningManage&action=ajaxSetDefaultAccount';
    /*
     * 查询过滤
     */

    
    //加载从后台读取的数据
    //gridStore.load();

    //var temp=Ext.DomQuery.select('body');
    //定义form的字段
    
    var filters = new Ext.grid.GridFilters({
      filters:[
        {type: 'string',  dataIndex: 'reckBewrite'},
        {type: 'string',  dataIndex: 'reckType'},
        {type: 'numeric', dataIndex: 'moneyNum'},
        {type: 'date',  dataIndex: 'consumeDate'},
        {type: 'string', dataIndex: 'reckExplain'},
        {type: 'boolean', dataIndex: 'inOrOut',yesText:'收入',noText:'支出'}
    ]});
    
    
    
    //accountStore.load();
    
    Ext.ux.RechoningGrid=function(config){
        
        this.setDefaultAccountUrl=config.setDefaultAccountUrl;
        this.accountStore=new Ext.data.Store( {
            proxy : new Ext.data.HttpProxy( {
                url : config.accountUrl
            }),
            reader : new Ext.data.JsonReader( {}, [ {
                name : 'uniqueCode'
            }, {
                name : 'accountName'
            }])
        });
        Ext.ux.RechoningGrid.superclass.constructor.call(this,config);
    }
    
    
    Ext.extend(Ext.ux.RechoningGrid,Ext.ux.GridExtend,{
        freeze:function(params){        
            this.sendId(this.freezeUrl,params);
        },
        setDefaultAccount:function(){
            var value=Ext.getCmp('accountCombo').getValue();
            if(value==undefined||value==''){
                 Ext.MessageBox.alert('消息','请选择账号!');
            }else{
                 this.ajaxRequest(this.setDefaultAccountUrl,{uniqueCode:value});
            }
        },
        reloadStore:function(){
            this.accountStore.reload();
        },
        resetFilter:function(){
            this.filters.clearFilters();
        },
        getForm:function(){
            //错误信息放在右边
            Ext.form.Field.prototype.msgTarget = 'side';
            //var formSaveUrl=this.formSaveUrl;
            //var formFields=this.formFields;
            var formSaveUrl=Ext.clone(this.formSaveUrl);
            var uniqueCode=new Ext.form.Hidden({
                name:'uniqueCode'
            });
            
            var reckBewrite=new Ext.form.TextField({
                fieldLabel:'帐单描述',
                allowBlank:false,
                name:'reckBewrite',
                anchor:'80%'
            });
            var accountTypeStore=new Ext.data.Store( {
                proxy : new Ext.data.HttpProxy( {
                    url : "../hf/gridBase.do"
                }),
                reader : new Ext.data.JsonReader( {}, [ {
                    name : 'uniqueCode'
                }, {
                    name : 'text'
                }]),
                baseParams :{
                	'action':'ajaxComboList',
                	'clazz':'TabType',
                	'obj':"{parentId:'10001'}"
                }
            });
            var accountType =new Ext.form.ComboBox({
                emptyText:'请选择帐单类型...',
                title:'帐单类型',
                fieldLabel:'帐单类型',
                mode:'remote',
                name:'reckType',
                triggerAction : 'all',
                valueField : 'uniqueCode',
                displayField: 'text',
                store:accountTypeStore,        
                readOnly : true,
                disabled : true, 
                resizable : true,
                itemCls:'stop-float',
                anchor:'80%'
             })
            var radioOut = new Ext.form.Radio({
                        boxLabel:'支出',
                        labelWidth:0,
                        hideLabel:true,
                        name:'inOrOut',
                        invalidText:'此项为必选选项',  
                        showInvalidText:true,       //在此控件后添加错误提示
                        labelSeparator:'',
                        layout:'column',
                        inputValue:'0'
                    });
            radioOut.on({
                'check':function(radio,value){
                	if(value){
                        accountType.enable();
                        accountTypeStore.baseParams.obj="{parentId:'10001'}" ;
                        accountTypeStore.load();
                        accountType.store = accountTypeStore;
                	}
                	
                }
            });
            var formFields=[
                    uniqueCode,
                    reckBewrite,                
                    new Ext.form.Radio({
                        fieldLabel:'收入/支出',
                        allowBlank:false,
                        boxLabel:'收入',
                        clearCls:'allow-float',     //允许两边浮动
                        itemCls:'float-left',       //向左浮动
                        name:'inOrOut',
                        inputValue:'1',
                        layout:'column',
                        listeners:{
                        	'check':function(radio,value){
                        		if(value){
                                    accountType.enable();
                                    accountTypeStore.baseParams.obj="{parentId:'10002'}" ;                            
                                    accountTypeStore.load({
                                        baseParams:accountTypeStore.baseParams
                                    });
                                    accountType.store = accountTypeStore;
                                }
                        	}
                        		
                        } 
                    }),
                    radioOut,
                    accountType,
                    new Ext.form.NumberField({
                        fieldLabel:'金额',
                        anchor:'80%',
                        allowBlank:false,
                        value:0,
                        name:'moneyNum'
                    }),
                    new Ext.form.DateField({
                        fieldLabel:'帐单日期',
                        name:'consumeDate',
                        format:'m/d/Y',
                        anchor:'80%'
                    }),
                    new Ext.form.TextArea({
                        fieldLabel:'帐单说明',
                        name:'reckExplain',
                        disableKeyFilter:true, 
                        anchor:'80%'
                    })
                ];
            //实例化form面板
            var form=new Ext.form.FormPanel({//start Ext.form.FormPanel
                baseCls:'x-plain',
                url:this.formSaveUrl,
                autoScroll:true, 
                frame:true,
                id:'form',
                items:formFields
            });//end Ext.form.FormPanel
            
            return form;
        }
    });

    var grid=new Ext.ux.RechoningGrid({
        baseUrl:baseUrl,
        editUrl:editUrl,
        deleteUrl:deleteUrl,
        listUrl:listUrl,
        formSaveUrl:formSaveUrl,
        el:'grid',
        filters:filters,
        freezeUrl:freezeUrl,
        accountUrl:accountUrl,
        setDefaultAccountUrl:setDefaultAccountUrl,      
        cmDataArray:[{
            header:'帐单描述',              //列名称
            sortable: true,
            dataIndex:'reckBewrite'     //数据对应名称
        },{
            sortable: true,
            header:'收入/支出',              //列名称
            dataIndex:'inOrOut',         //数据对应名称
            renderer:function(value){
                if(value=='0'){
                    return '支出';
                }else{
                    return '收入';
                }
            }
        },{
            sortable: true,
            header:'帐单类型',              //列名称
            dataIndex:'reckType'            //数据对应名称        
        },{
            sortable: true,
            header:'金额 ',             //列名称
            dataIndex:'moneyNum',           //数据对应名称
            renderer: Ext.util.Format.usMoney
        },{
            sortable: true,
            header:'帐单日期',              //列名称
            dataIndex:'consumeDate'           //数据对应名称
        },{
            sortable: true,
            header:'帐单说明',              //列名称
            dataIndex:'reckExplain'           //数据对应名称        
        }]
    }).render(); 
    
});